home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_nansi / nan24hyc.arc / nansi.sup < prev    next >
Text File  |  1990-10-31  |  9KB  |  273 lines

  1. /***** hpcvmb:net.sources / topaz!dpz /  7:53 am  May 12, 1986*/
  2.  
  3. Support Routines for nansi.sys
  4. The three files setraw.asm, setraw.lc, and setraw.msc contain routines
  5. to allow use of RAW mode from assembly, Lattice C, and Microsoft C.
  6.  
  7. Lattice C is a little wierder than most, so rawtest.lc was included
  8. to show how to use setraw.lc; the other languages require no special
  9. setup to use setraw.
  10.  
  11. ;---- setraw.asm ----------------------------------------------
  12.     public    getraw, setraw
  13. ;----- dos ----------------------
  14. ; Call DOS function # n.
  15. dos    macro    fn
  16.     mov    ah, fn
  17.     int    21h
  18.     endm
  19.  
  20. code    segment para public 'CODE'
  21. assume cs:code
  22. ;----- Getraw ---------------------------------------------
  23. ; Returns AX=1 if file BX is a device in raw mode; 0 otherwise.
  24. ; Returns Carry set & errorcode in AX if DOS error.
  25.  
  26. getraw    proc    near
  27.     mov    al, 0
  28.     DOS    44h        ; Get attributes
  29.     jc    gr_exit        ; bad file handle?
  30.     mov    ax, 20h
  31.     and    al, dl        ; get that bit
  32.     mov    cl, 4
  33.     shr    ax, cl
  34.     clc
  35. gr_exit:
  36.     ret
  37. getraw    endp
  38.  
  39. ;----- Setraw -------------------------------------------
  40. ; Sets Raw state of file BX to (AX != 0) if file is a device.
  41. ; Returns Carry set & errorcode in AX if DOS error.
  42. setraw    proc    near
  43.     mov    cx, ax
  44.     mov    al, 0
  45.     DOS    44h
  46.     jc    sr_exit
  47.     test    al, 80h        ; It it a device?
  48.     jz    sr_exit        ; nope- do nothing.
  49.     ; Current mode in DX; set CX = 20H if CX nonzero.
  50.     or    cx, cx
  51.     jz    sr_ax0
  52.         mov    cx, 20h
  53. sr_ax0: and    dx, 00cfh    ; clear old raw bit and hi byte,
  54.     or    dx, cx        ; set new value.
  55.     mov    al, 1
  56.     DOS    44h
  57. sr_exit:
  58.     ret
  59. setraw    endp
  60. code    ends
  61.     end
  62. ;---- end of setraw.asm ------------------------------------------
  63. /*--- setraw.msc -------------------------------------------------
  64.   Routines to set and reset raw mode on stdin/stdout.
  65.   For Microsoft C.
  66. ------------------------------------------------------------------*/
  67. #include <dos.h>
  68. /* Use the IOCTL DOS function call to change stdin and stdout to raw mode.
  69.  * For stdin, this prevents MSDOS from trapping ^P, ^S, ^C, thus freeing us
  70.  * of ^P toggling 'echo to printer'.
  71.  * For stdout, this radically speeds up the output because there is no
  72.  * checking for these special characters in the input buffer whenever
  73.  * screen output is occurring.
  74.  * Note that only the stdin OR stdout ioctl need be changed since
  75.  * apparently they are handled as the same device.
  76.  * Thanks to Mark Zbikowski (markz@microsoft.UUCP) for helping me with
  77.  * this.
  78.  * --- This stolen from sources to the mighty game HACK ---
  79.  */
  80. #define DEVICE        0x80
  81. #define RAW        0x20
  82. #define IOCTL        0x44
  83. #define STDIN        fileno(stdin)
  84. #define STDOUT        fileno(stdout)
  85. #define GETBITS        0
  86. #define SETBITS        1
  87. static unsigned    old_stdin, old_stdout, ioctl();
  88. /*--- set_raw() ----------
  89.   Call this to set raw mode; call restore_raw() later to restore
  90.   console to old rawness state.
  91. --------------------------*/
  92. set_raw()
  93. {
  94.     old_stdin = ioctl(STDIN, GETBITS, 0);
  95.     old_stdout = ioctl(STDOUT, GETBITS, 0);
  96.     if (old_stdin & DEVICE)
  97.         ioctl(STDIN, SETBITS, old_stdin | RAW);
  98.     if (old_stdout & DEVICE)
  99.         ioctl(STDOUT, SETBITS, old_stdout | RAW);
  100. }
  101. restore_raw()
  102. {
  103.     if (old_stdin)
  104.         (void) ioctl(STDIN, SETBITS, old_stdin);
  105.     if (old_stdout)
  106.         (void) ioctl(STDOUT, SETBITS, old_stdout);
  107. }
  108. static unsigned
  109. ioctl(handle, mode, setvalue)
  110. unsigned setvalue;
  111. {
  112.     union REGS regs;
  113.  
  114.     regs.h.ah = IOCTL;
  115.     regs.h.al = mode;
  116.     regs.x.bx = handle;
  117.     regs.h.dl = setvalue;
  118.     regs.h.dh = 0;            /* Zero out dh */
  119.     intdos(®s, ®s);
  120.     return (regs.x.dx);
  121. }
  122. /*-- end of setraw.msc --*/
  123. /*------ setraw.lc ----------------------------------------------
  124. Lattice C routines which get and set the current raw/cooked state
  125. of a file, given its Lattice file descriptor.
  126. Useful when trying to obtain high console output speeds.
  127. ----------------------------------------------------------------*/
  128.  
  129. #include "\lc\dos.h"
  130. #define CARRY 0x1
  131. #define ERROR (-1)
  132. #define TRUE 1
  133. #define FALSE 0
  134.  
  135. extern _oserr;
  136.  
  137. /*---- getraw --------------------------------------------------
  138. Returns TRUE if file fd is a device in raw mode; FALSE otherwise.
  139. Returns ERROR, puts errorcode in _oserr, if DOS error.
  140. ----------------------------------------------------------------*/
  141. getraw(fd)
  142. int fd;
  143. {
  144.     union REGS inregs;
  145.     union REGS outregs;
  146.     int    flags;
  147.  
  148.     if (fd > 2) fd+=2;        /* convert to DOS fd */
  149.     inregs.x.bx = fd;
  150.     inregs.x.ax = 0x4400;        /* get file attributes */
  151.     flags = intdos(&inregs, &outregs);
  152.     if (flags & CARRY) {
  153.         _oserr = outregs.x.ax;
  154.         return -1;
  155.     }
  156.     return (outregs.x.dx & 0x20) ? TRUE : FALSE;
  157. }
  158.  
  159. /*---- setraw --------------------------------------------------
  160. Sets Raw state of file fd to raw_on (if file is not a device, does nothing).
  161. Returns zero if successful.
  162. Returns ERROR & errorcode in _oserr if DOS error.
  163. ----------------------------------------------------------------*/
  164. setraw(fd, raw_on)
  165. int fd, raw_on;
  166. {
  167.     union REGS inregs;
  168.     union REGS outregs;
  169.     int    flags;
  170.  
  171.     if (fd > 2) fd+=2;        /* convert to DOS fd */
  172.  
  173.     inregs.x.ax = 0x4400;        /* get file attributes */
  174.     inregs.x.bx = fd;
  175.     flags = intdos(&inregs, &outregs);
  176.     if (flags & CARRY) {
  177.         _oserr = outregs.x.ax;
  178.         return ERROR;
  179.     }
  180.     if ((outregs.x.ax & 0x80) == 0)    /* return zero if not device */
  181.         return 0;
  182.  
  183.     outregs.x.ax = 0x4401;        /* set file attributes */
  184.     outregs.x.bx = fd;
  185.     outregs.x.dx &= 0xcf;        /* clear old raw bit & hi byte */
  186.     if (raw_on) outregs.x.dx |= 0x20;    /* maybe set new raw bit */
  187.     flags = intdos(&outregs, &inregs);
  188.     if (flags & CARRY) {
  189.         _oserr = inregs.x.ax;
  190.         return ERROR;
  191.     }
  192.     return 0;
  193. }
  194. /*-- end of setraw.lc --*/
  195. /*------ rawtest.lc -------------------------------------------------
  196. Lattice C program to demonstrate the difference in speed between
  197. DOS's raw and cooked modes when writing to the DOS console.
  198. Requires setraw.c; make the demo as follows:
  199.     lc setraw rawtest
  200.     link \lc\s\c rawtest setraw,rawtest,,\lc\s\lc
  201. and run it by typing
  202.     rawtest
  203.  
  204. Note- Lattice C's raw mode (i.e. using mode "rb" or "wb" with fopen)
  205. is not the same as DOS's raw mode, and does not affect speed.
  206.  
  207. What does affect speed is whether output is performed with single-character
  208. DOS calls, which is the default.  To get a speed improvement, you must 
  209. open "con" WITHOUT a trailing colon, and use that file for high-speed output;
  210. see section 5.2 (Device I/O) of the Lattice manual.
  211.  
  212. When using MS-DOS raw mode, the console is in totally unbuffered mode-
  213. echo is turned off, no printer echoing is done, and no line editing
  214. is done, regardless of which file setraw was applied to.  This means
  215. that the console must be in non-raw ("cooked") mode for line-oriented
  216. console input to work properly.
  217.  
  218. Note: no speed difference will be noticed when using the standard console
  219. driver ANSI.SYS that comes with DOS; you must be using NANSI.SYS to
  220. get massively fast output.  
  221. To use nansi.sys, insert the following line in \config.sys:
  222.     device = nansi.sys
  223. and put nansi.sys on the top level directory; the system will load
  224. it at boot time.
  225. (If there was already a line invoking plain old ansi.sys, remove it.)
  226. --------------------------------------------------------------------*/
  227.  
  228. #include "\lc\stdio.h"
  229.  
  230. char    response[128];
  231.  
  232. main(argc, argv)
  233. int    argc;
  234. char    **argv;
  235. {
  236.     FILE    *fp;
  237.     int    fd;
  238.     int    old_rawness;
  239.     int    i;
  240.  
  241.     fp = fopen("con", "w");
  242.     if (fp == NULL) fprintf(stderr, "can't open 'con'!");
  243.     fd = fileno(fp);        /* get Level 1 file descriptor */
  244.     old_rawness = getraw(fd);    /* Save old raw/cooked state */
  245.  
  246.     setraw(fd, 0);            /* make sure we're in cooked mode */
  247.  
  248.     puts("Cooked mode test (hit return):");
  249.     gets(response);
  250.     fprintf(fp, "\033[2J");        /* clear screen */
  251.     for (i=0; i<20; i++)
  252.         fprintf(fp, "This is cooked mode!  Why is it so darned slow? %d\n", i);
  253.     fflush(fp);
  254.  
  255.     puts("Raw mode test (hit return):");
  256.     gets(response);            /* must be in cooked mode to use gets */
  257.     setraw(fd, 1);            /* enter raw mode */
  258.     fprintf(fp, "\033[2J");        /* clear screen */
  259.     for (i=0; i<20; i++)
  260.         fprintf(fp, "-- This is raw mode- it's clearly faster than cooked! %d\n", i);
  261.     fflush(fp);            /* finish writing while in raw mode */
  262.     setraw(fd, old_rawness);    /* go back to old raw/cooked state  */
  263.  
  264.     fclose(fp);
  265. }
  266. /*--- end of rawtest.lc ---*/
  267. -- 
  268. Name: David P. Zimmerman    Nickname: "Davidann" (don't ask)
  269. Cute quote: " (well, *I* think it's cute!)
  270. Arpa: dzimmerman@blue.rutgers.edu
  271. Uucp: ...{harvard, allegra, seismo}!topaz!dpz
  272. /* ---------- */
  273.